package com.zyylr.classroomreservationsystem.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.zyylr.classroomreservationsystem.R;
import com.zyylr.classroomreservationsystem.others.CustomerAdapter;
import com.zyylr.classroomreservationsystem.others.bean.Cell;
import com.zyylr.classroomreservationsystem.others.bean.ColTitle;
import com.zyylr.classroomreservationsystem.others.bean.RowTitle;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;


import cn.zhouchaoyuan.excelpanel.ExcelPanel;

public class ShowFreeTimeActivity extends MyBaseCompatActivity implements ExcelPanel.OnLoadMoreListener {
    public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
    public static final String WEEK_FORMAT_PATTERN = "EEEE";
    public static final String DATE_FORMAT_START = "HH:mm";
    public static final long ONE_DAY = 24 * 3600 * 1000;
    public static final long time_30 = 30 * 60 * 1000;
    public static final int PAGE_SIZE = 14;
    public static final int ROW_SIZE = 48;

    private ExcelPanel excelPanel;
    private ProgressBar progress;
    private CustomerAdapter adapter;
    private List<RowTitle> rowTitles;
    private List<ColTitle> colTitles;
    private List<List<Cell>> cells;
    private SimpleDateFormat dateFormatPattern;
    private SimpleDateFormat weekFormatPattern;
    private boolean isLoading;
    private long moreStartTime;
    private String startTime;     //00:00整


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_free_time);
        progress = (ProgressBar) findViewById(R.id.progress);
        excelPanel = (ExcelPanel) findViewById(R.id.content_container);
        adapter = new CustomerAdapter(getApplicationContext(), blockListener);
        excelPanel.setAdapter(adapter);
        excelPanel.setOnLoadMoreListener(this);
        //excelPanel.addOnScrollListener(onScrollListener);
        initData();


    }

    private View.OnClickListener blockListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Cell cell = (Cell) view.getTag();
            if (cell != null) {
                if (cell.getStatus() == 0) {
                    Toast.makeText(getApplicationContext(), "空闲", Toast.LENGTH_SHORT).show();
                } else if (cell.getStatus() == 1) {
                    Toast.makeText(getApplicationContext(), "已被预约", Toast.LENGTH_SHORT).show();
                } else if (cell.getStatus() == 2) {
                    Toast.makeText(getApplicationContext(), "待审核", Toast.LENGTH_SHORT).show();
                }
            }
        }
    };


    @Override
    public void onLoadMore() {


    }

    @Override
    public void onLoadHistory() {


    }

    private void initData() {
        moreStartTime = Calendar.getInstance().getTimeInMillis();
        //historyStartTime = moreStartTime - ONE_DAY * PAGE_SIZE;
        dateFormatPattern = new SimpleDateFormat(DATE_FORMAT_PATTERN);
        weekFormatPattern = new SimpleDateFormat(WEEK_FORMAT_PATTERN);
        rowTitles = new ArrayList<>();
        colTitles = new ArrayList<>();
        cells = new ArrayList<>();
        for (int i = 0; i < ROW_SIZE; i++) {
            cells.add(new ArrayList<Cell>());
        }
        loadData(moreStartTime, false);
    }

    private void loadData(long startTime, final boolean history) {
        //模拟网络加载
        isLoading = true;
        Message message = new Message();
        message.arg1 = history ? 1 : 2;
        message.obj = new Long(startTime);
        loadDataHandler.sendMessageDelayed(message, 1000);
    }

    private Handler loadDataHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            isLoading = false;
            long startTime = (Long) msg.obj;
            List<RowTitle> rowTitles1 = genRowData(startTime);
            List<List<Cell>> cells1 = genCellData();
//            if (msg.arg1 == 1) {//history
//                historyStartTime -= ONE_DAY * PAGE_SIZE;
//                rowTitles.addAll(0, rowTitles1);
//                for (int i = 0; i < cells1.size(); i++) {
//                    cells.get(i).addAll(0, cells1.get(i));
//                }

//                //加载了数据之后偏移到上一个位置去
//                if (excelPanel != null) {
//                    excelPanel.addHistorySize(PAGE_SIZE);
//                }
            //           } else
            if (msg.arg1 == 2) {
                moreStartTime += ONE_DAY * PAGE_SIZE;
                rowTitles.addAll(rowTitles1);
                for (int i = 0; i < cells1.size(); i++) {
                    cells.get(i).addAll(cells1.get(i));
                }
            }
            if (colTitles.size() == 0) {
                colTitles.addAll(genColData());
            }
            progress.setVisibility(View.GONE);
            adapter.setAllData(colTitles, rowTitles, cells);
//            adapter.enableFooter();
//            adapter.enableHeader();
        }
    };

    private List<RowTitle> genRowData(long startTime) {
        List<RowTitle> rowTitles = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < PAGE_SIZE; i++) {
            RowTitle rowTitle = new RowTitle();
            rowTitle.setAvailableRoomCount(random.nextInt(10) + 10);
            rowTitle.setDateString(dateFormatPattern.format(startTime + i * ONE_DAY));
            rowTitle.setWeekString(weekFormatPattern.format(startTime + i * ONE_DAY));
            rowTitles.add(rowTitle);
        }
        return rowTitles;
    }

    private List<ColTitle> genColData() {
        List<ColTitle> colTitles = new ArrayList<>();
        Calendar calender = Calendar.getInstance();
        calender.set(Calendar.HOUR_OF_DAY, 00);
        calender.set(Calendar.MINUTE, 00);
        Date date = new Date();
        //long start = calender.getTimeInMillis();

        //String date_1 = new SimpleDateFormat(DATE_FORMAT_START).format(date);
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_START);

//        startTime = "00:00";
//        dateFormatPattern = new SimpleDateFormat(DATE_FORMAT_START);
//        long startTime_1 = Long.parseLong(startTime);
        for (int i = 0; i < 48; i++) {
            ColTitle colTitle = new ColTitle();
//            colTitle.setRoomNumber(Long.toString(startTime_1 + time_30*i));
            if (i < 10) {
                colTitle.setRoomNumber(format.format(new Date(date.getTime())));
            } else {
                colTitle.setRoomNumber("12:00");
            }


            colTitles.add(colTitle);
        }
        return colTitles;
    }

    private List<List<Cell>> genCellData() {
        List<List<Cell>> cells = new ArrayList<>();
        for (int i = 0; i < 48; i++) {
            List<Cell> cellList = new ArrayList<>();
            cells.add(cellList);
            for (int j = 0; j < PAGE_SIZE; j++) {
                Cell cell = new Cell();
                Random random = new Random();
                int number = random.nextInt(6);

                if (number == 0) {
                    cell.setStatus(number);
                    cell.setStatus_name("空闲");
                }

                if (number == 1) {
                    cell.setStatus(number);
                    cell.setStatus_name("被预定");

                }

                if (number == 2) {
                    cell.setStatus(number);
                    cell.setStatus_name("待审核");
                }

                cellList.add(cell);
            }
        }
        return cells;
    }
}
